package com.campus.merchant.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.common.bean.common.PageUtil;
import com.campus.merchant.entity.MerchantDailyEarnings;
import com.campus.merchant.mapper.MerchantDailyEarningsMapper;
import com.campus.merchant.vo.earnings.EarningsQueryParam;
import com.campus.merchant.vo.earnings.EarningsRecordVo;
import com.campus.merchant.vo.earnings.EarningsStatisticsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户每日收益汇总表 服务实现类
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-07-11
 */
@Service
public class MerchantDailyEarningsService extends ServiceImpl<MerchantDailyEarningsMapper, MerchantDailyEarnings> {

    /**
     * 获取商户昨日收益
     */
    public BigDecimal getYesterdayEarnings(Long merchantId) {
        LambdaQueryWrapper<MerchantDailyEarnings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantDailyEarnings::getMerchantId, merchantId)
                   .eq(MerchantDailyEarnings::getEarningDate, LocalDate.now().minusDays(1));
        
        MerchantDailyEarnings yesterdayRecord = this.getOne(queryWrapper);
        return yesterdayRecord != null ? yesterdayRecord.getTotalEarnings() : BigDecimal.ZERO;
    }

    /**
     * 获取商户累计收益
     */
    public BigDecimal getTotalEarnings(Long merchantId) {
        LambdaQueryWrapper<MerchantDailyEarnings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantDailyEarnings::getMerchantId, merchantId)
                   .select(MerchantDailyEarnings::getTotalEarnings);
        
        List<MerchantDailyEarnings> records = this.list(queryWrapper);
        return records.stream()
                .map(MerchantDailyEarnings::getTotalEarnings)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 获取商户本月收益
     */
    public BigDecimal getMonthEarnings(Long merchantId) {
        LocalDate now = LocalDate.now();
        LocalDate monthStart = now.withDayOfMonth(1);
        
        LambdaQueryWrapper<MerchantDailyEarnings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantDailyEarnings::getMerchantId, merchantId)
                   .ge(MerchantDailyEarnings::getEarningDate, monthStart)
                   .le(MerchantDailyEarnings::getEarningDate, now)
                   .select(MerchantDailyEarnings::getTotalEarnings);
        
        List<MerchantDailyEarnings> records = this.list(queryWrapper);
        return records.stream()
                .map(MerchantDailyEarnings::getTotalEarnings)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    /**
     * 获取商户收益统计
     */
    public EarningsStatisticsVo getEarningsStatisticsVo(Long merchantId) {
        EarningsStatisticsVo vo = new EarningsStatisticsVo();
        
        // 昨日收益
        vo.setYesterdayEarnings(getYesterdayEarnings(merchantId));
        
        // 本月收益
        vo.setMonthEarnings(getMonthEarnings(merchantId));
        
        // 累计收益
        vo.setTotalEarnings(getTotalEarnings(merchantId));
        
        return vo;
    }

    /**
     * 获取商户收益记录列表
     */
    public PageInfo<EarningsRecordVo> getEarningsRecords(EarningsQueryParam param) {
        LambdaQueryWrapper<MerchantDailyEarnings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MerchantDailyEarnings::getMerchantId, param.getMerchantId());
        
        if (param.getStartDate() != null) {
            queryWrapper.ge(MerchantDailyEarnings::getEarningDate, param.getStartDate());
        }
        if (param.getEndDate() != null) {
            queryWrapper.le(MerchantDailyEarnings::getEarningDate, param.getEndDate());
        }
        
        queryWrapper.orderByDesc(MerchantDailyEarnings::getEarningDate);
        
        PageInfo<MerchantDailyEarnings> pageInfo = PageHelper.startPage(param.getPageNo(), param.getPageSize())
                .doSelectPageInfo(() -> list(queryWrapper));
        
        List<EarningsRecordVo> voList = pageInfo.getList().stream()
                .map(this::convertToVo)
                .collect(Collectors.toList());
        
        return PageUtil.convertPageInfo(pageInfo, voList);
    }

    /**
     * 转换实体对象为VO对象
     */
    private EarningsRecordVo convertToVo(MerchantDailyEarnings entity) {
        EarningsRecordVo vo = new EarningsRecordVo();
        BeanUtil.copyProperties(entity, vo);
        return vo;
    }
}
